package com.lkd.handler;

import com.google.common.collect.Lists;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.BaseContract;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskDetailsViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@Topic(TopicConfig.TASK_SUPPLY_TOPIC)//TopicConfig.VMS_SUPPLY_TOPIC
@Slf4j
public class VmSupplyHandler implements MsgHandler {
    @Autowired
    private TaskService taskService;
    @Autowired
    VMService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //解析
        SupplyContract supplyContract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
        //参数校验
        if (supplyContract == null) {
            log.info("发送协议的信息不能为空");
            throw new LogicException("发送协议的信息为空");
        }
        String innerCode = supplyContract.getInnerCode();
        List<SupplyChannel> supplyData = supplyContract.getSupplyData();
        if (CollectionUtils.isEmpty(supplyData) || innerCode == null) {
            log.info("发送协议的信息innerCode为空/supplyData为空");
            throw new LogicException("发送协议的信息innerCode为空/supplyData为空");
        }
        //查询售货机
        VmVO vmInfo = vmService.getVMInfo(innerCode);
        if (vmInfo == null) {
            log.info("当前机器不存在,机器码" + innerCode);
            throw new LogicException("当前机器不存在" + innerCode);
        }
        //获得当前最小工单数的人员
        int userId = taskService.getLeastTsakUser(vmInfo.getRegionId(), false);
        if (userId != 0) {
            //封装创建任务工单的参数信息
            TaskViewModel taskViewModel = new TaskViewModel();
            taskViewModel.setCreateType(0);
            taskViewModel.setInnerCode(innerCode);
            taskViewModel.setUserId(userId);
            taskViewModel.setAssignorId(0);
            taskViewModel.setProductType(VMSystem.TASK_TYPE_SUPPLY);
            taskViewModel.setDesc("自动创建");

            //对捕获信息进行封装
            List<TaskDetailsViewModel> list = new ArrayList<>();
            supplyData.forEach(supplyChannel -> {
                TaskDetailsViewModel taskDetailsViewModel = new TaskDetailsViewModel();
                taskDetailsViewModel.setChannelCode(supplyChannel.getChannelId());
                taskDetailsViewModel.setExpectCapacity(supplyChannel.getCapacity());
                taskDetailsViewModel.setSkuId(supplyChannel.getSkuId());
                taskDetailsViewModel.setSkuName(supplyChannel.getSkuName());
                taskDetailsViewModel.setSkuImage(supplyChannel.getSkuImage());
                list.add(taskDetailsViewModel);
            });
            taskViewModel.setDetails(list);
            log.info("自动创建补货工单");
            taskService.createTask(taskViewModel);
        }
    }
}
